package com.qf.dao.impl;

import com.qf.dao.RegistrationDao;
import com.qf.entity.Registration;
import com.qf.entity.RegistrationVo;
import com.qf.utils.DBUtils;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RegistrationDaoImpl implements RegistrationDao {

    @Override
    public List<Registration> selectAllRegistration(RegistrationVo registrationVo, Integer currentPageNo, Integer pageSize) throws SQLException {
        StringBuffer sb  = new StringBuffer("SELECT  r.*,d.name AS dname  FROM  register   r  INNER JOIN  doctor  d  \n" +
                "ON r.did = d.did WHERE  1=1  ");
        //定义一个集合来存储占位符的值
        List<Object>  parmList = new ArrayList<>();
        if (registrationVo.getRid() !=null && !"".equals(registrationVo.getRid())){
            sb.append(" and r.rid =?");
            parmList.add(registrationVo.getRid());
        }

        if (registrationVo.getDname() !=null && !"".equals(registrationVo.getDname())){
            sb.append(" and d.name  like ?");
            parmList.add("%"+registrationVo.getDname()+"%");
        }
        if (registrationVo.getStartTime() !=null && !"".equals(registrationVo.getStartTime())){
            sb.append(" and r.registerDate > ?");
            parmList.add(registrationVo.getStartTime());
        }

        if (registrationVo.getEndTime() !=null && !"".equals(registrationVo.getEndTime())){
            sb.append(" and r.registerDate < ?");
            parmList.add(registrationVo.getEndTime());
        }

        //拼接分页
        sb.append(" limit ?,?");
        parmList.add((currentPageNo-1)*pageSize);
        parmList.add(pageSize);
        List<Registration> registrationList = DBUtils.DB.qr.query(sb.toString(), new BeanListHandler<Registration>(Registration.class), parmList.toArray());
        return registrationList;
    }


    @Override
    public Integer selectCountRegistration(RegistrationVo registrationVo) throws SQLException {
        StringBuffer sb  = new StringBuffer("SELECT  count(1)  FROM  register   r  INNER JOIN  doctor  d  \n" +
                "ON r.did = d.did WHERE  1=1  ");
        //定义一个集合来存储占位符的值
        List<Object>  parmList = new ArrayList<>();
        if (registrationVo.getRid() !=null && !"".equals(registrationVo.getRid())){
            sb.append(" and r.rid =?");
            parmList.add(registrationVo.getRid());
        }

        if (registrationVo.getDname() !=null && !"".equals(registrationVo.getDname())){
            sb.append(" and d.name  like ?");
            parmList.add("%"+registrationVo.getDname()+"%");
        }
        if (registrationVo.getStartTime() !=null && !"".equals(registrationVo.getStartTime())){
            sb.append(" and r.registerDate > ?");
            parmList.add(registrationVo.getStartTime());
        }

        if (registrationVo.getEndTime() !=null && !"".equals(registrationVo.getEndTime())){
            sb.append(" and r.registerDate < ?");
            parmList.add(registrationVo.getEndTime());
        }


        Long count =null;
        //对集合进行判断
        if (parmList !=null && parmList.size() >0){
            count= (Long) DBUtils.DB.qr.query(sb.toString(),new ScalarHandler(),parmList.toArray());
        }else{
            count= (Long) DBUtils.DB.qr.query(sb.toString(),new ScalarHandler());
        }


        return count ==null ? 0 : Integer.parseInt(count+"");
    }

}

